package Service;

import Mapper.MouthlyBudgetMapper;
import pojo.MouthBudget;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.util.Scanner;

public class MouthlyBudgetService {
    private MouthlyBudgetMapper mouthlyBudgetMapper = new MouthlyBudgetMapper();
    private Scanner scanner = new Scanner(System.in);

    public boolean setMouthlyBudget() throws SQLException {
        String currentMouthly = LocalDate.now().toString().substring(0, 7);
        //将获取的时间 ， 截取年份以及月份
        System.out.println("当前的月份是： " + currentMouthly);
        double mouthlyBudget = mouthlyBudgetMapper.getMouthlyBudget(currentMouthly);
        System.out.println("当前的月份的金额是"+mouthlyBudget);
        if (mouthlyBudget != 0) {
            System.out.println("当前的月度的金额是： " + mouthlyBudget);
            System.out.println("当前月度的金额已经存在， 是否要修改 （1， 表示修改， 0 ，表示不修改）");
            int choose = scanner.nextInt();
            switch (choose) {
                case 1: {
                    System.out.println("请输入设置的预算的金额 ：");
                    double money = scanner.nextDouble();
                    int i = mouthlyBudgetMapper.updateMonthklyBudget(money, currentMouthly);
                    if (i > 0) {
                        System.out.println("修改月度金额成功");
                        return true;

                    }
                    System.out.println("修改失败");
                    return false;

                }
                case 2: {
                    System.out.println("你当前的选择是不修改当前的月度金额");
                    return false;
                }
            }

        } else {
            System.out.println("当前的情况是当前的月度金额不存在 ， 是否要新建一个月度金额");
            System.out.println("1表示新建一个月度金额 ，  0表示取消");
            int choose = scanner.nextInt();
            switch (choose) {
                case 1: {
                    System.out.println("请输入对应的月份的金额");
                    double monthMouth = scanner.nextDouble();
                    Timestamp create = new Timestamp(System.currentTimeMillis());
                    Timestamp update = new Timestamp(System.currentTimeMillis());
                    MouthBudget mouthBudget = new MouthBudget();
                    mouthBudget.setCreate(create);
                    mouthBudget.setUpdate(update);
                    mouthBudget.setBudget_amount(monthMouth);
                    int i = mouthlyBudgetMapper.insertMouthlyBudget(monthMouth, currentMouthly, mouthBudget);
                    if (i > 0) {
                        System.out.println("新建一个月度金额成功");
                        return true;

                    }
                    System.out.println("新建一个月度金额失败");
                    return false;


                }

            }
        }
        return false;

    }
}
